layui.use(['admin', 'ax', 'table', 'treetable'], function () {
    var $ax = layui.ax;
    var admin = layui.admin;
    var table = layui.table;
    var treetable = layui.treetable;

    var Current = {
        tableId: "table"
    };

    /**
     * 初始化表格的列
     */
    Current.initColumn = function () {
        return [[
            {type: 'numbers'},
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'name', sort: true, title: '菜单名称'},
            {field: 'status', sort: true, title: '状态' ,templet: function (d) {
                    return d.status == 0 ? "禁用" : "启用";
                }
            },
            {field: 'description', sort: true, title: '描述'},
            {field: 'showMode', hide: true, sort: true, title: '展示方式'},
            {field: 'menuFlag', hide: true, sort: true, title: '菜单标识'},
            {field: 'url', hide: true, sort: true, title: '菜单地址'},
            {field: 'code', hide: true, sort: true, title: '菜单编码'},
            {field: 'sort', hide: true, sort: true, title: '菜单排序'},
            {field: 'icon', hide: true, sort: true, title: '菜单名称'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    /**
     * 初始化表格
     */
    Current.initTable = function (tableId, data) {
        return treetable.render({
            elem: '#' + tableId,
            url: system.contextPath + '/admin/menu/search/all',
            where: data,
            page: false,
            height: "full-158",
            cellMinWidth: 100,
            cols: Current.initColumn(),
            treeColIndex: 2,
            treeSpid: "0",
            treeIdName: "id",
            treePidName: "pid",
            treeDefaultClose: true,
            treeLinkage: false
            ,isJump: true //是否允许点击节点时弹出新窗口跳转
            ,showLine: true  //是否开启连接线
        });
    };

    // 渲染表格
    var tableResult = Current.initTable(Current.tableId);

    $('#expandAll').click(function () {
        treetable.expandAll('#' + Current.tableId);
    });

    $('#foldAll').click(function () {
        treetable.foldAll('#' + Current.tableId);
    });

    Current.onUse = function (data) {
        var menuData = {
            menuId: data.id ,
            menuName: data.name,
            menuStatus: data.status,
            menuDescription: data.description ,
            menuUrl : data.url ,
            menuShowMode: data.showMode ,
            menuFlag : data.menuFlag ,
            menuIcon : data.icon ,
            menuSort : data.sort ,
            menuCode : data.code
        }
        console.log(menuData);
        system.useSelectorData(menuData);
    };

    // 工具条点击事件
    table.on('tool(' + Current.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if(layEvent === 'use'){
            Current.onUse(data);
        }
    });
});
